package me.jiatao.wan.test

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView

/**
 * @Author: taojia
 * @CreateDate: 2021/8/25 12:37 下午
 * @Version: 1.0
 * @Description:
 */

class TestPagingAdapter : PagingDataAdapter<String, TestPagingAdapter.ViewHolder>(diffCallback) {
    companion object {
        val diffCallback: DiffUtil.ItemCallback<String> = object : DiffUtil.ItemCallback<String>() {
            override fun areItemsTheSame(oldItem: String, newItem: String): Boolean {
                return oldItem == newItem
            }

            override fun areContentsTheSame(oldItem: String, newItem: String): Boolean {
                return oldItem == newItem
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(parent)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindTo(getItem(position))
    }


    class ViewHolder(parent: ViewGroup) :
        RecyclerView.ViewHolder(LayoutInflater.from(parent.context).inflate(android.R.layout.simple_list_item_1, parent, false)) {
        private val titleView = itemView.findViewById<TextView>(android.R.id.text1)

        fun bindTo(item: String?) {
            titleView.text = item ?: "Null Value"
        }
    }
}